﻿Ext.define("Ext.ux.comboboxtree", {
    extend: "Ext.form.field.ComboBox",
    requires: ["Ext.tree.Panel"],
    sValue:"",
    initComponent: function () {
        var self = this;
        Ext.apply(self, {
            fieldLabel: self.fieldLabel,
            labelWidth: self.labelWidth
        });
        self.callParent();
    },
    createPicker: function () {
        var self = this;
        var store = Ext.create('Ext.data.TreeStore', {
            proxy: {
                type: 'ajax',
                url: self.storeUrl
            },
            sorters: [{
                property: 'leaf',
                direction: 'ASC'
            }, {
                property: 'text',
                direction: 'ASC'
            }],
            root: {
                id: self.rootId,
                text: self.rootText
            },
            nodeParameter: self.treeNodeParameter
        });
        self.picker = new Ext.tree.Panel({
            height: 300,
            autoScroll: true,
            floating: true,
            focusOnToFront: false,
            shadow: true,
            ownerCt: this.ownerCt,
            useArrows: true,
            store: store,
            rootVisible: false,
            listeners: {
                'selectionchange': function (node, records, eOpts) {
                    //var records = self.picker.getChecked(),
                    var names = [], values = [];
                    Ext.Array.each(records, function (rec) {
                       // if (rec.get('leaf') == true) {
                            names.push(rec.get(self.displayField));
                            values.push(rec.get(self.valueField));
                       // }
                    });
                    if (values.length > 0) {

                        self.setRawValue(values.join(';')); // 隐藏值
                        self.setValue(names.join(';')); // 显示值
                        //var abc = self.getRawValue;
                       // console.log("getRawValue" + self.getRawValue);
                        //console.log("getValue" + self.getValue);
                       // console.log("getRawValue" + self.getRawValue());
                        // console.log("getValue" + self.getValue());
                        self.sValue = values.join(';');
                        //console.log("getValueeeeeee" + self.sValue)
                    }

                }

            }
        });

        return self.picker;
    },
    alignPicker: function () {
        var me = this, picker, isAbove, aboveSfx = '-above';
        if (this.isExpanded) {
            picker = me.getPicker();
            if (me.matchFieldWidth) {
                picker.setWidth(me.bodyEl.getWidth());
            }
            if (picker.isFloating()) {
                picker.alignTo(me.inputEl, "", me.pickerOffset); // ""->tl
                isAbove = picker.el.getY() < me.inputEl.getY();
                me.bodyEl[isAbove ? 'addCls' : 'removeCls'](me.openCls
      + aboveSfx);
                picker.el[isAbove ? 'addCls' : 'removeCls'](picker.baseCls
      + aboveSfx);
            }
        }
    },
    getRawValue: function () {
        var me = this;
        return me.sValue;
    }

});